$( document ).on( "pageshow", "#myFlower", function( event ) {
	load_myFlowerInfo();
});

$( document ).on( "pagecreate", "#myFlower", function( event ) {
	hideWXToolBar();
	
	//点击红花详情
	$( document ).on( "click", "#flowerDetail", function( event ) {	
		goTo(rootPath+"/jsp/weixin/myFlowerInfo.jsp?access_token="+access_token);
	});
	//我送给了谁更多
	$( document ).on( "click", "#more_mySend_myFlower", function( event ) {	
		goTo(rootPath+"/jsp/weixin/mySendFlowerMore.jsp?access_token="+access_token);
	});
	//谁送给了我更多
	$( document ).on( "click", "#more_sendMe_myFlower", function( event ) {	
		goTo(rootPath+"/jsp/weixin/sendMyFlowerMore.jsp?access_token="+access_token);
	});
});

function load_myFlowerInfo(){
	access_token=getQueryString("access_token");
	$.ajax({
	  	url: rootPath+"/user/personInfo.do",
	  	type: "POST",
	  	dataType: "text",
		data: "access_token="+access_token,
	  	success: function( response ) {
	  		var data=$.parseJSON(response);
	  		
			if(data.state=="200"){
		  		var user=data.data;
		  		var userTelephone=user.telephone==undefined||user.telephone==null? "":user.telephone;
		  		var userWorkEmail=user.work_email==undefined||user.work_email==null? "":user.work_email;
		  		var sendFlowerNum=user.sendFlowerNum==undefined||user.sendFlowerNum==null? "0":user.sendFlowerNum;
		  		var userDeptName=user.deptName==undefined||user.deptName==null? "":user.deptName;
		  		$("#userName_myFlower").text(user.empName);
		  		$("#userGrade_myFlower").text(user.integralGrade);
		  		$("#userIntegralNum_myFlower").text(user.integralNum);
		  		$("#userFlowerNum_myFlower").text(parseInt(user.redFlowerNum+user.fdFlowerNum));
		  		$("#userDeptName_myFlower").text(userDeptName);
		  		$("#userTelephone_myFlower").text(userTelephone);
		  		$("#userWorkEmail_myFlower").text(userWorkEmail);
		  		
		  		$("#sendFlowerNum_myFlower").text(parseInt(sendFlowerNum));
		  		$("#addFlowerNum_myFlower").text(parseInt(user.redFlowerNum));
		  		$("#fdFlowerNum_myFlower").text(parseInt(user.fdFlowerNum));
		  		
		  		var picSrc ="/images/weixin/flower/flower_06.png";
		  		var picNum = 0;
		  		if(parseInt(user.fdFlowerNum*10%10/2)==1){
		  			picSrc = "/images/weixin/flower/flower_01.png";
		  			picNum = 1;
		  		}else if(parseInt(user.fdFlowerNum*10%10/2)==2){
		  			picSrc = "/images/weixin/flower/flower_02.png";
		  			picNum = 2;
		  		}else if(parseInt(user.fdFlowerNum*10%10/2)==3){
		  			picSrc = "/images/weixin/flower/flower_03.png";	
		  			picNum = 3;
		  		}else if(parseInt(user.fdFlowerNum*10%10/2)==4){
		  			picSrc = "/images/weixin/flower/flower_04.png";
		  			picNum = 4;
		  		}
		  		$("#fdFlowerPetalNum_myFlower").html("<img src="+rootPath+picSrc+"><span >"+picNum+"</span> 瓣");
		  		$("#needPetalNum_myFlower").text(5-parseInt(user.fdFlowerNum*10%10/2));	
		  		var canSendNumOfToday=user.canSendNumOfToday;
		  		if(canSendNumOfToday=="-1"){//无限制赠送
		  			var canPic='<img src="'+rootPath+flower_+'"/>';
		  			$("#canSengNumShow_myInfo").html(canPic+"现在送花无限制，此时不送更待何时！");
		  		}else{
			  		var flowNum = 0;
			  		if(canSendNumOfToday>0){
				  		var canPic="";
				  		if(canSendNumOfToday>3){
				  			flowNum = 2;
				  		}else{
				  			flowNum = canSendNumOfToday;
				  		}
//				  		for(var i=0;i<flowNum;i++){
				  			canPic='<img src="'+rootPath+flower_+'"/>';
//				  		}
				  		$("#canSengNumShow_myFlower").html(canPic+"还有[ "+canSendNumOfToday+" ]次赠送机会哦！");
			  		}else{
			  			$("#canSengNumShow_myFlower").html("今天的赠送机会用完咯！");
			  		}
		  		}
		  		if(!canSendNumOfToday&&canSendNumOfToday==0){
		  			$("#btn_go_send_myFlower").hide();
		  			$("#btn_go_send2_myFlower").hide();
		  		}else{
		  			//绑定页面跳转事件
		  			bindPageTo_SearchEmp();
		  		}
		  			
		  		
		  		var userHeadPic=user.headImageUrl==undefined||user.headImageUrl==null? rootPath+defaultHeadPic :user.headImageUrl;
		  		
		  		$("#userHead_myFlower").attr("src",userHeadPic).show();
		  		var flowerUrl="";
		  		if(user.redflowerState=="死掉"){
		  			flowerUrl=flower_die;
		  		}else if(user.redflowerState=="枯萎"){
		  			flowerUrl=flower_droop;
		  		}else if(user.redflowerState=="盛开"){
		  			flowerUrl=flower_bloom;
		  		}
		  		$("#userFlowerStatePic_myFlower").attr("src",rootPath+flowerUrl);
		  		//显示我的送花列表
		  		showMySendFlowerListFun();
		  		//显示送我花的列表
		  		showSendMeFlowerListFun();
			}else{
				alert(data.msg);
			}
	  	}
	});
}

function bindPageTo_SearchEmp(){
	$( document ).on( "click", "#btn_go_send_myFlower,#btn_go_send2_myFlower", function( event ) {
		goTo(rootPath+"/jsp/weixin/searchEmp.jsp?access_token="+access_token);
	});
}
/**
 * 显示我的送花列表
 */
function showMySendFlowerListFun(){
	$.ajax({
	  	url: rootPath+"/redFlower/mySendFloweList.do",
	  	type: "POST",
	  	dataType: "text",
		data: "access_token="+access_token+"&pageNo="+1+"&pageSize="+6,
	  	success: function( response ) {
	  		var data=$.parseJSON(response);
			if(data.state=="200"){
				var arr=data.data.rows;
				if(arr.length<=0 || data.data.totalPage==undefined || data.data.totalPage==1){
		  			$("#more_mySend_myFlower").hide();
		  		}
		  		
					var content = "";
					var p = 0;
					if(arr.length>6){
						p = 6;
					}else {
						p = arr.length;
					}
					for (var i=0;i<p;i++){
						var headerPic=rootPath+defaultHeadPic;
						if(arr[i].addManHeadImgUrl!=undefined && arr[i].addManHeadImgUrl!="")
							headerPic=arr[i].addManHeadImgUrl;
						content +='<dl>\
							<dt><img src="'+headerPic+'" /></dt>\
							<dd>'+arr[i].addManName+'</dd>\
						</dl>';
					}				
					$("#showMySendList_myFlower").html("").append(content);
		  		
			}else{
//				alert(data.msg);
			}
	  	}
	});
}
/**
 * 显示送我花的列表
 */
function showSendMeFlowerListFun(){
	$.ajax({
		url: rootPath+"/redFlower/sendMeFloweList.do",
	  	type: "POST",
	  	dataType: "text",
		data: "access_token="+access_token+"&pageNo="+1+"&pageSize="+6,
	  	success: function( response ) {
	  		var data=$.parseJSON(response);
			if(data.state=="200"){
		  		var arr=data.data.rows;
		  		if(arr.length<=0 || data.data.totalPage==undefined || data.data.totalPage==1){
		  			$("#more_sendMe_myFlower").hide();
		  		}
					var content = "";
					var p = 0;
					if(arr.length>6){
						p = 6;
					}else {
						p = arr.length;
					}
					for (var i=0;i<p;i++){
						var headerPic=rootPath+systemHeadPic;
						var name = arr[i].sendManName=='admin'?"系统":arr[i].sendManName;
						if(arr[i].sendManHeadImgUrl!=undefined && arr[i].sendManHeadImgUrl!="")
							headerPic=arr[i].sendManHeadImgUrl;
						content +='<dl>\
							<dt><img src="'+headerPic+'" /></dt>\
							<dd>'+name+'</dd>\
						</dl>';
					}
					$("#showSendMeList_myFlower").html("").append(content);
		  		
			}else{
//				alert(data.msg);
			}
	  	}
	});
}